package com.incar.chuxing.activity.pay;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.incar.chuxing.R;
import com.incar.chuxing.activity.MainActivity;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlContainer;
import com.incar.chuxing.application.MyApplication;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.ResultBean;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.ImageLoader;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.Logger;
import com.incar.chuxing.utils.ToastUtils;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PayDoneActivity extends BaseActivity {
    @BindView(R.id.pay_done_image)
    ImageView payDoneImage;
    private Context mContext = this;
    private String orderId;
    private String carId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_pay_done;
    }

    @Override
    public void initView() {
        switch ((int) (new Date().getTime() % 4)) {
            case 0:
                ImageLoader.getInstance().displayImageNotCache(getApplicationContext(), MyApplication.getInstance().getImageUrl() + "201712261501", payDoneImage);
                break;
            case 1:
                ImageLoader.getInstance().displayImageNotCache(getApplicationContext(), MyApplication.getInstance().getImageUrl() + "201712261502", payDoneImage);
                break;
            case 2:
                ImageLoader.getInstance().displayImageNotCache(getApplicationContext(), MyApplication.getInstance().getImageUrl() + "201712261503", payDoneImage);
                break;
            case 3:
                ImageLoader.getInstance().displayImageNotCache(getApplicationContext(), MyApplication.getInstance().getImageUrl() + "201712261504", payDoneImage);
                break;
        }
    }


    @Override
    public void initData() {
        orderId = getIntent().getStringExtra(IntentKey.INTENT_ORDER_ID);
        carId = getIntent().getStringExtra(IntentKey.INTENT_CAR_ID);
    }

    @OnClick({R.id.pay_done_main, R.id.pay_done_close_car})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pay_done_main:
                Intent intent = new Intent(mContext, MainActivity.class);
                intent.putExtra("jumpCode", 7);
                startActivity(intent);
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.pay_done_close_car:
                //重复锁车
                Logger.i("click close door again");
                closeDoorAgain();
                break;
        }
    }

    private void closeDoorAgain() {
        showLoadingDialog();
        ApiStore.getInstance().getApiService(UrlContainer.Request.BASE_GATE_URL).closeDoorAgain(MyApplication.getInstance().getRequestFieldMap(), orderId, carId)
                .enqueue(new Callback<ResultBean>() {
                    @Override
                    public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                        dismissLoadingDialog();
                        if (response.isSuccessful() && response.body() != null) {
                            if ("200".equals(response.body().getStatus())) {
                                ToastUtils.makeText(mContext, response.body().getMessage(), 0, ToastUtils.LENGTH_SHORT).show();
                            } else {
                                ToastUtils.makeText(mContext, response.body().getMessage(), ToastUtils.LENGTH_SHORT).show();
                            }
                        } else {
                            ToastUtils.makeText(mContext, getResources().getString(R.string.serverError), ToastUtils.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultBean> call, Throwable t) {
                        dismissLoadingDialog();
                        ToastUtils.makeText(mContext, getResources().getString(R.string.badNetwork), ToastUtils.LENGTH_SHORT).show();
                    }
                });

    }

    @Override
    public void onBackPressed() {

    }
}
